<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/1/18
 * Time: 15:08
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class SeminarBagModel
 * @package SDXApi\Model
 */
class SeminarTemplateBagModel extends Model
{
	/**
	 * @var string
	 */
	protected $tableName = 'seminar_template_bag';

	/**
	 * Created by Jerry
	 * @param $templateId
	 * @return bool|mixed
	 */
	public function get_seminar_template_bag_by_id($templateId)
	{
		if (empty($templateId)) {
			return false;
		}

		$where = array(
			'template_id'	=>	$templateId,
		);

		return $this->where($where)->find();
	}

    /**
     * @author Jerry
     * @param int $templateId
     * @return mixed
     */
    public function findSeminarTemplateBagsByTemplateId($templateId = 0)
    {
        $where = array(
            'b.template_id'     =>  $templateId,
//            'bt.send_end_date'  =>  array('LT', $time),
        );

        $field = 'b.id, b.bag_id, b.sort, bt.type_money, bt.min_goods_amount, bt.type_name, bt.recharge_amount, bt.min_amount, bt.use_start_date, bt.use_end_date, bt.type_id, bt.limit_send_num, bt.bonus_discount_type, bt.send_type, bt.send_start_date, bt.send_end_date, bt.effective_days, bt.has_effective_days';
        $join = 'LEFT JOIN sdx_bonus_type as bt ON b.bag_id = bt.type_id';

        $data = $this
                    ->field($field)
                    ->alias('b')
                    ->join($join)
                    ->where($where)
                    ->select();

        return $data;
    }

    /**
     * @author Jerry
     * @param int $typeId
     */
    public function findSeminarTemplateBagsUserByTypeId($typeId = 0)
    {
        $where = array(
            'ub.bonus_type_id'    => $typeId,
        );

        $field = '';
        $join = 'left join sdx_users u on ub.user_id = u.user_id';

        $data = $this->table('sdx_user_bonus')
                        ->field($field)
                        ->alias('ub')
                        ->join($join)
                        ->where($where)
                        ->select(false);

        dd($data);
    }

    /**
     * @author Jerry
     * @param int $bag
     * @return mixed
     */
    public function findBagNotSendNumber($bag = 0)
    {
        $where = array(
            'user_id'       =>  0,
            'bonus_type_id' =>  $bag,
        );

        return $this->table('sdx_user_bonus')->where($where)->count();
    }

    /**
     * @author Jerry
     * @param int $userId
     * @param int $bag_id
     * @return mixed
     */
    public function findBagIsUser($userId = 0, $bag_id = 0)
    {
        $where = array(
            'user_id'       =>  $userId,
            'bonus_type_id' =>  $bag_id,
        );

        return $this->table('sdx_user_bonus')->where($where)->count();
    }


    /**
     * @author Jerry
     * @param int $bag
     * @return mixed
     */
    public function findBagUserNumber($bag = 0)
    {
        $where = array(
            'user_id'       =>  array('NEQ', 0),
            'bonus_type_id' =>  $bag,
        );

        return $this->table('sdx_user_bonus')->where($where)->count();
    }
}